package com.kele.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/**
 * @Version 2019
 * @Author:kele
 * @Date:2021/4/16
 * @Content:
 * @Description：数据库链接工具包
 */
public class CollectionUtils {
    //驱动
    private static String driver;
    //地址
    private static String url;
    //数据库链接名
    private static String username;
    //数据库连接密码
    private static String password;

    /**
     * 获取数据库连接对象
     * @return connection
     */
    public static Connection getConnection()  {

        //读取db.properties配置文件
        Properties properties = new Properties();
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
        try {

            properties.load(inputStream);
            driver = properties.getProperty("driverName");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");

            //注册驱动
            Class.forName(driver);
            return DriverManager.getConnection(url, username, password);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

